<template>
  <el-container class="home_container">
    <!-- 头部区 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="info">退出</el-button></el-header
    >
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="istoggle ? '64px' : '200px'">
        <!-- 设置左侧菜单折叠展开功能 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- unique-opened：是否只保持一个子菜单的展开 -->
        <!-- collapse-transition：是否开启折叠动画 -->
        <!--router: 启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
        <!-- default-active:默认激活菜单的 index -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="istoggle"
          :collapse-transition="false"
          :router="true"
          :default-active="activepath"
        >
          <!-- 一级菜单-->
          <!-- 绑定唯一的:index='index',使一级菜单点那个打开哪个而不是，点开一个就全部打开  -->
          <el-submenu
            v-for="(item, index) in menulist"
            :key="item.id"
            :index="index + ''"
          >
            <!-- 一级菜单模板区域 -->
            <template #title>
              <!-- 图标 -->
              <i :class="iconsObj[index]"></i>
              <!-- 文本 -->
              <span class="onetext"> {{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              v-for="subitem in item.children"
              :key="subitem.id"
              :index="'/' + subitem.path"
              @click="saveNavState('/' + subitem.path)"
            >
              <template #title>
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span class="twotext">{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      // 设置iconsObj数组，以便可以遍历循环
      iconsObj: {
        0: "iconfont icon-user",
        1: "iconfont icon-tijikongjian",
        2: "iconfont icon-shangpin",
        3: "iconfont icon-danju",
        4: "iconfont icon-baobiao",
      },
      // 菜单时候展开或折叠
      istoggle: false,
      // 被激活的链接地址
      activepath: "",
    };
  },
  created() {
    this.getMenuList();
    // 当页面被创建时，将sessionstorage中的activepath赋值给activepath
    this.activepath = window.sessionStorage.getItem("activepath");
  },
  methods: {
    info() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    // 点击按钮切换菜单是否折叠展开
    toggleCollapse() {
      this.istoggle = !this.istoggle;
    },
    //  存放链接激活状态
    saveNavState(path) {
      window.sessionStorage.setItem("activepath", path);
    },
  },
};
</script>
<style lang='less' scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 20px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    img {
      margin-right: 15px;
    }
  }
}
.onetext {
  margin-left: 10px;
}
.twotext {
  margin-left: 10px;
}
.el-aside {
  background-color: #333744;
  .toggle-button {
    background-color: #4a5064;
    text-align: center;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    // 设置两个字之间的间距
    letter-spacing: 0.25em;
    cursor: pointer;
  }
  .el-menu {
    // 解决el-menu侧边没对齐的问题
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>